#include "monitorwindow.h"
#include "ui_monitorwindow.h"
#include "deviceinfowindow.h"
#include <QMessageBox>
#include <QCloseEvent>
#include <QDateTime>

MonitorWindow::MonitorWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MonitorWindow)
{
    ui->setupUi(this);
    
    // 设置窗口属性
    setWindowFlags(Qt::Window | Qt::WindowCloseButtonHint | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
    
    setupConnections();
    
    // 初始化界面
    ui->tabWidget->setCurrentIndex(0);
}

MonitorWindow::~MonitorWindow()
{
    delete ui;
}

void MonitorWindow::setupConnections()
{
    connect(ui->realTimeMonitorButton, &QPushButton::clicked, this, &MonitorWindow::onRealTimeMonitorClicked);
    connect(ui->deviceStatusButton, &QPushButton::clicked, this, &MonitorWindow::onDeviceStatusClicked);
    connect(ui->alarmButton, &QPushButton::clicked, this, &MonitorWindow::onAlarmClicked);
    connect(ui->statisticsButton, &QPushButton::clicked, this, &MonitorWindow::onStatisticsClicked);
    connect(ui->deviceInfoButton, &QPushButton::clicked, this, &MonitorWindow::onDeviceInfoClicked);
    connect(ui->refreshButton, &QPushButton::clicked, this, &MonitorWindow::onRefreshClicked);
    connect(ui->closeButton, &QPushButton::clicked, this, &MonitorWindow::onCloseClicked);
}

void MonitorWindow::onRealTimeMonitorClicked()
{
    QString info = QString("实时监控功能\n\n"
                          "当前时间: %1\n\n"
                          "功能说明:\n"
                          "• 实时显示售货机运行状态\n"
                          "• 监控设备温度、湿度等参数\n"
                          "• 显示当前库存情况\n"
                          "• 监控网络连接状态\n\n"
                          "注：此功能正在开发中，敬请期待...")
                   .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    
    updateDetailInfo(info);
    ui->tabWidget->setCurrentIndex(1);
}

void MonitorWindow::onDeviceStatusClicked()
{
    QString info = QString("设备状态检查\n\n"
                          "检查时间: %1\n\n"
                          "设备组件状态:\n"
                          "• 主控制器: 正常\n"
                          "• 显示屏: 正常\n"
                          "• 支付模块: 正常\n"
                          "• 出货机构: 正常\n"
                          "• 温控系统: 正常\n"
                          "• 网络模块: 正常\n\n"
                          "注：此功能正在开发中，实际状态需要连接真实设备...")
                   .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    
    updateDetailInfo(info);
    ui->tabWidget->setCurrentIndex(1);
}

void MonitorWindow::onAlarmClicked()
{
    QString info = QString("报警信息查看\n\n"
                          "查询时间: %1\n\n"
                          "报警记录:\n"
                          "• 暂无报警信息\n\n"
                          "报警类型说明:\n"
                          "• 设备故障报警\n"
                          "• 库存不足报警\n"
                          "• 温度异常报警\n"
                          "• 网络断开报警\n"
                          "• 支付异常报警\n\n"
                          "注：此功能正在开发中，敬请期待...")
                   .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    
    updateDetailInfo(info);
    ui->tabWidget->setCurrentIndex(1);
}

void MonitorWindow::onStatisticsClicked()
{
    QString info = QString("统计分析报告\n\n"
                          "统计时间: %1\n\n"
                          "销售统计:\n"
                          "• 今日销售额: ¥0.00\n"
                          "• 今日销售量: 0 件\n"
                          "• 本月销售额: ¥0.00\n"
                          "• 本月销售量: 0 件\n\n"
                          "热销商品:\n"
                          "• 暂无数据\n\n"
                          "设备运行时间:\n"
                          "• 累计运行: 0 小时\n"
                          "• 故障次数: 0 次\n\n"
                          "注：此功能正在开发中，需要连接数据库获取真实数据...")
                   .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    
    updateDetailInfo(info);
    ui->tabWidget->setCurrentIndex(1);
}

void MonitorWindow::onDeviceInfoClicked()
{
    DeviceInfoWindow *deviceWindow = new DeviceInfoWindow(nullptr);  // 创建为独立窗口
    deviceWindow->show();
}

void MonitorWindow::onRefreshClicked()
{
    QString info = QString("数据刷新\n\n"
                          "刷新时间: %1\n\n"
                          "刷新内容:\n"
                          "• 设备状态数据\n"
                          "• 实时监控数据\n"
                          "• 报警信息\n"
                          "• 统计数据\n\n"
                          "刷新完成！\n\n"
                          "注：此功能正在开发中...")
                   .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
    
    updateDetailInfo(info);
    ui->tabWidget->setCurrentIndex(1);
    
    QMessageBox::information(this, "刷新完成", "监控数据已刷新！");
}

void MonitorWindow::onCloseClicked()
{
    this->close();
}

void MonitorWindow::updateDetailInfo(const QString &info)
{
    ui->detailTextEdit->setPlainText(info);
}

void MonitorWindow::closeEvent(QCloseEvent *event)
{
    emit windowClosed();
    event->accept();
}